#pragma once
#include "main.h"

template <typename T> 
class Grid{
public:
	Grid():isInit(false){}

	Grid(int x, int y, T initialValue) : width(x), height(y), size(x*y),isInit(true){
		Initialize(initialValue);
	}

	virtual ~Grid()	{DELETE(arr);}

	void Set(T right){
		int count = size;
		while (count--)	{
				arr[count] = right;
		}
	}

	// Returns the index from x and y
	size_t index(int x, int y)	{return x+width*y;}
	// Returns the value in the Grid from an x and y coordinate
	T& element(int x, int y)	{return arr[x+width*y];}
	// Returns the value in the Grid from the 1D coordinate
	T& element1D(int i)			{return arr[i];}

	int GetX()          { return width;	}
	void SetX(int setX) { width = setX;		sizechanged = true;}
	int GetY()          { return height;	}
	void SetY(int setY) { height = setY;	sizechanged = true;}
	int GetSize()		{ return size;	}
	bool hasInit()		{ return isInit;}

protected:
	// Creates and initializes the Grid 
	void Initialize(T initialValue)	{
		int isize = (size > 0 ? size : 0);
		arr = new T[isize];
		Set(initialValue);
	}
	T* arr;
	int width;
	int height;
	int size;
	bool sizechanged;
	bool isInit;
};
